<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ISBN" prop="isbn">
        <el-input
          v-model="queryParams.isbn"
          placeholder="请输入ISBN"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入书名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="定价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入定价"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="馆藏位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入馆藏位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['book_info:book_info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['book_info:book_info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['book_info:book_info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['book_info:book_info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="book_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图书ID" align="center" prop="bookId" />
      <el-table-column label="ISBN" align="center" prop="isbn" />
      <el-table-column label="书名" align="center" prop="title" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="出版社" align="center" prop="publisher" />
      <el-table-column label="出版年份" align="center" prop="publicationYear" />
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="定价" align="center" prop="price" />
      <el-table-column label="图书状态" align="center" prop="status" />
      <el-table-column label="馆藏位置" align="center" prop="location" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['book_info:book_info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['book_info:book_info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图书信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="book_infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ISBN" prop="isbn">
          <el-input v-model="form.isbn" placeholder="请输入ISBN" />
        </el-form-item>
        <el-form-item label="书名" prop="title">
          <el-input v-model="form.title" placeholder="请输入书名" />
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" />
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-input v-model="form.category" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="定价" prop="price">
          <el-input v-model="form.price" placeholder="请输入定价" />
        </el-form-item>
        <el-form-item label="馆藏位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入馆藏位置" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">借阅记录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddBorrowRecord">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteBorrowRecord">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="borrowRecordList" :row-class-name="rowBorrowRecordIndex" @selection-change="handleBorrowRecordSelectionChange" ref="borrowRecord">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="读者ID" prop="readerId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.readerId" placeholder="请输入读者ID" />
            </template>
          </el-table-column>
          <el-table-column label="借阅日期" prop="borrowDate" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.borrowDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择借阅日期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="应还日期" prop="dueDate" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.dueDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择应还日期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="实际归还日期" prop="returnDate" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.returnDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择实际归还日期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="罚款金额" prop="fine" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.fine" placeholder="请输入罚款金额" />
            </template>
          </el-table-column>
          <el-table-column label="借阅状态" prop="status" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.status" placeholder="请选择借阅状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作人" prop="operator" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.operator" placeholder="请输入操作人" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdAt" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.createdAt"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择创建时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updatedAt" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.updatedAt"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择更新时间">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Book_info">
import { listBook_info, getBook_info, delBook_info, addBook_info, updateBook_info } from "@/api/book_info/book_info"

const { proxy } = getCurrentInstance()

const book_infoList = ref([])
const borrowRecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedBorrowRecord = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    isbn: null,
    title: null,
    author: null,
    publisher: null,
    publicationYear: null,
    category: null,
    price: null,
    status: null,
    location: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    isbn: [
      { required: true, message: "ISBN不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "书名不能为空", trigger: "blur" }
    ],
    author: [
      { required: true, message: "作者不能为空", trigger: "blur" }
    ],
    publisher: [
      { required: true, message: "出版社不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询图书信息列表 */
function getList() {
  loading.value = true
  listBook_info(queryParams.value).then(response => {
    book_infoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    bookId: null,
    isbn: null,
    title: null,
    author: null,
    publisher: null,
    publicationYear: null,
    category: null,
    price: null,
    status: null,
    location: null,
    createdAt: null,
    updatedAt: null
  }
  borrowRecordList.value = []
  proxy.resetForm("book_infoRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.bookId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加图书信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _bookId = row.bookId || ids.value
  getBook_info(_bookId).then(response => {
    form.value = response.data
    borrowRecordList.value = response.data.borrowRecordList
    open.value = true
    title.value = "修改图书信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["book_infoRef"].validate(valid => {
    if (valid) {
      form.value.borrowRecordList = borrowRecordList.value
      if (form.value.bookId != null) {
        updateBook_info(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBook_info(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _bookIds = row.bookId || ids.value
  proxy.$modal.confirm('是否确认删除图书信息编号为"' + _bookIds + '"的数据项？').then(function() {
    return delBook_info(_bookIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 借阅记录序号 */
function rowBorrowRecordIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 借阅记录添加按钮操作 */
function handleAddBorrowRecord() {
  let obj = {}
  obj.readerId = ""
  obj.borrowDate = ""
  obj.dueDate = ""
  obj.returnDate = ""
  obj.fine = ""
  obj.status = ""
  obj.operator = ""
  obj.remark = ""
  obj.createdAt = ""
  obj.updatedAt = ""
  borrowRecordList.value.push(obj)
}

/** 借阅记录删除按钮操作 */
function handleDeleteBorrowRecord() {
  if (checkedBorrowRecord.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的借阅记录数据")
  } else {
    const borrowRecords = borrowRecordList.value
    const checkedBorrowRecords = checkedBorrowRecord.value
    borrowRecordList.value = borrowRecords.filter(function(item) {
      return checkedBorrowRecords.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleBorrowRecordSelectionChange(selection) {
  checkedBorrowRecord.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('book_info/book_info/export', {
    ...queryParams.value
  }, `book_info_${new Date().getTime()}.xlsx`)
}

getList()
</script>
